package com.hmall.item.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
      PageDTO<Item> pageDTO =   itemService.pageAll(page,size);
      return pageDTO;
    }
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
       return itemService.getById(id);
    }
    @PostMapping
    public void insertAll(@RequestBody Item item){
        itemService.save(item);
    }
    @PutMapping("/status/{id}/{status}")
    public void upDateStatus(@PathVariable Long id,@PathVariable Integer status){
        Item item = itemService.getById(id);
        item.setStatus(status);
        itemService.updateById(item);
        String itkey = status==1?"items.up":"items.down";
        rabbitTemplate.convertAndSend("ittopic",itkey,id);

    }

    @PutMapping
    public void upDateAll(@RequestBody Item item){
       itemService.updateById(item);
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable Long id){
        itemService.removeById(id);
    }
    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.stock(itemId,num);
    }

}
